package com.iweb.day11;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author WuYongheng
 * @date 2021/11/28
 * @description
 */
public class Test2 {
    public static void main(String[] args) {
        {
            Connection conn = null;
            Statement statement = null;
            Scanner scanner = new Scanner(System.in);
            // 数据库连接信息
            String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
            String username = "hr";
            String password = "123456";
            try {
                // 加载驱动
                Class.forName("oracle.jdbc.driver.OracleDriver");
                // 连接对象
                conn = DriverManager.getConnection(url, username, password);
                if (conn != null) {
                    System.out.println("连接成功");
                }
                System.out.print("请输入年纪编号：");
                int gradeId = scanner.nextInt();
                System.out.print("请输入年纪名称：");
                String gradeName = scanner.next();
                // 使用StringBuilder 拼凑
                StringBuilder sb = new StringBuilder();
                sb.append("insert into grade values(");
                sb.append(gradeId);
                sb.append(",'");
                sb.append(gradeName);
                sb.append("')");

                // sql 语句
//                String sql = "update student set phone='6666688888'" +
//                        "where studentno=1001";
//                String sql = "delete from student  where studentno=1000";
                String sql = sb.toString();
                // 创建Statement 对象
                statement = conn.createStatement();

                int result = statement.executeUpdate(sql);
                if (result > 0) {
                    System.out.println("修改成功！");
                } else {
                    System.out.println("修改失败！");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                // 释放资源
                try {
                    if (statement != null) {
                        statement.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
